var FormDropzone = function () {


    return {
        //main function to initiate the module
        init: function () {  
            Dropzone.options.myDropzone = {
                init: function() {
                    this.on("addedfile", function(file) {/*
                    	//console.log(file);
                    	//console.log(file.xhr);
                        // Create the remove button
                        var removeButton = Dropzone.createElement("<button type='button' class='btn btn-sm btn-block'>删除图片</button>");
                        
                        // Capture the Dropzone instance as closure.
                        var _this = this;

                        // Listen to the click event
                        removeButton.addEventListener("click", function(e) {
                          // Make sure the button click doesn't submit the form:
                          e.preventDefault();
                          e.stopPropagation();
                          console.log(file);
                          // Remove the file preview.
                          _this.removeFile(file);
                          // If you want to the delete the file on the server as well,
                          // you can do the AJAX request here.
                        });

                        // Add the button to the file preview element.
                        file.previewElement.appendChild(removeButton);
                    */});
                    this.on("removedfile", function(file) {
                    	//console.log(file);
                    });
                    this.on("success", function(file, data) {
                    	//console.log(data);
                    	var removeButton = Dropzone.createElement("<div class='clearfix'><button type='button' name='remove_image_btn' class='btn btn-xs  red' mval='"+data[0].id+"'>删除</button><button type='button' class='btn btn-xs  blue' name='set_cover_btn' mval='"+data[0].id+"'>设为封面</button></div>");
                        
                        // Capture the Dropzone instance as closure.
                        var _this = this;

                        // Listen to the click event
                        $("[name='remove_image_btn']").live("click", function(e) {
                          var id=$(this).attr("mval");
                          // Make sure the button click doesn't submit the form:
                          e.preventDefault();
                          e.stopPropagation();
                          console.log(file);
                          // Remove the file preview.
                          _this.removeFile(file);
                          delImage(id);
                          // If you want to the delete the file on the server as well,
                          // you can do the AJAX request here.
                        });

                        // Add the button to the file preview element.
                        file.previewElement.appendChild(removeButton);
                    });
                   
                }/*,
	            success: function(file, data) {
	            	console.log(data);
	
	            }*/
          }
            
            //实例化已有图片
            $("[name='drop_del_btn']").click(function(){
            	 var $inner_modal_submit=$("#inner_modal_submit");
            	 $inner_modal_submit.modal();
            	var tempView=$(this);
            	var id=$(this).attr("mval");
            	$.ajax({
            		url:'product/delProductImage.do',
            		data:"imgId="+id,
            		type:'post',
            		success:function(data){
            			 $inner_modal_submit.modal("hide");
            			toastr.options.positionClass = "toast-top-center";
            			if("1"==data){
            				toastr["success"]("删除成功.", "操作提示");
            				tempView.parent('div').remove();
            			}else{
            				toastr["error"]("删除失败,请稍后重试.", "操作提示");
            			}
            		}
            	});
            });
            
            //点击更换封面
            $("[name='set_cover_btn']").live("click",function(){
            	 var $inner_modal_submit=$("#inner_modal_submit");
            	 $inner_modal_submit.modal();
            	var id=$(this).attr("mval");
            	var productCode=$("#productCode").val();
            	$.ajax({
            		url:'product/setCoverImage.do',
            		data:"imgId="+id+"&proCode="+productCode,
            		type:'post',
            		success:function(data){
            			$inner_modal_submit.modal("hide");
            			toastr.options.positionClass = "toast-top-center";
            			if("1"==data){
            				toastr["success"]("设置封面成功.", "操作提示");
            			}else{
            				toastr["error"]("设置失败,请稍后重试.", "操作提示");
            			}
            		}
            	});
            });
        }
    };
}();

function delImage(id){
	var $inner_modal_submit=$("#inner_modal_submit");
	$inner_modal_submit.modal();
	$.ajax({
		url:'product/delProductImage.do',
		data:"imgId="+id,
		type:'post',
		success:function(data){
			 $inner_modal_submit.modal("hide");
			toastr.options.positionClass = "toast-top-center";
			if("1"==data){
				toastr["success"]("删除成功.", "操作提示");
			}else{
				toastr["error"]("删除失败,请稍后重试.", "操作提示");
			}
		}
	});
}